<template>
  <!--登录表单的容器-->
  <div class="login_container">
    <!--登录区域-->
    <div class="login_box">
      <!--头像-->
      <div class="avatar_box">
        <img src="../assets/image/touxiang.gif">
      </div>
      <!--表单-->
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0px" class="login_form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名称">
            <svg-icon slot="prefix" icon-class="usernew" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入登录密码" >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <!--<div class="code_box">-->
            <el-input v-model="loginForm.code" placeholder="请输入验证码" style="width: 58%;float: left" class="code">
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <img :src="codeUrl" @click="getCode" alt="" style="width: 40%;float: right;height: 36px;border-radius: 3px;" >
          <!--</div>-->
        </el-form-item>
        <!--<el-form-item class="login_btn">
          <el-button type="primary" @click="submitForm('loginForm')">登陆</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>-->
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button size="medium" type="primary" style="width:100%;" @click="submitForm('loginForm')">
            <span>登 陆</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2020-2022 zhou.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>

import { getCodeImg } from "@/api/login";

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        username: '',
        password: '',
        code: '',
        rememberMe: false,
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      }
    };
  },
  components: {

  },
  beforeDestroy () {
    document.removeEventListener('keyup',this.handerKeyup)
  },
  created(){
    this.getCode(),
    document.addEventListener("keyup",this.handerKeyup)
  },
  methods: {
    handerKeyup(e){
      var keycode = document.all ? event.keyCode : e.which;
      if (keycode == 13) {
        this.submitForm();
      }
    },
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    submitForm() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  //background-color: aquamarine;
  background: url("../assets/image/timg.jpg")no-repeat;
  background-size:100% 100%;
}

.login_box {
  width: 400px;
  height: 350px;
  background-color: #FFFFFF;
  border-radius: 7px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box{
    width: 100px;
    height: 100px;
    border: 1px solid #EEEEEE;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    margin: -65px auto;
    background-color: #FFFFFF;

    img{
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #EEEEEE;
    }
  }

  .login_form{
    position: absolute;
    bottom: 0px;
    width: 100%;
    padding: 0px 20px;
    box-sizing: border-box;

    /*.login_btn{
      display: flex;
      justify-content: center;
    }*/

    /*.code_box{
      display: flex;
      .code{
        width: 60%;
        justify-content: left;
      }

      .code_img{
        width: 40%;
        height: 45px;
        justify-content: flex-end;
      }
    }*/
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.input-icon {
  height: 39px;
  width: 14px;
  margin-left: 2px;
}
</style>
